--
-- Author: ping.wang
-- Date: 2018-07-13 10:41:20
--
-- AuctionFilterMenuView
--

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.AuctionFilterMenu

function ClassRef:init()
    display.nodeRemoveForNotHit(self._root, function()
        if self:isVisible() then
            self:setVisible(false)
        end
    end)
    self:setBackGroundColorOpacity(0)

    local btn  = self._ui.Button_1
    self._btnHeight = btn:getContentSize().height
    self._btns = {btn }
end


---datalist = {{name="name1",id=1},{name="name2",id=2},{name="name3",id=3}}
---data = {dataList,pos,cb} 下拉条内容 位置 点击后的回调
function ClassRef:refresh(data)
    self._dataList = data.dataList
    self:refreshBtns(data.pos)
    self._cb = data.cb
end

function ClassRef:refreshBtns(pos)
    local size = self._ui.Panel:getContentSize()
    local height = #self._dataList * self._btnHeight
    self._ui.Panel:setContentSize(cc.size(size.width, height))
    self._ui.bg:setContentSize(cc.size(size.width, height))
    self._root:setContentSize(cc.size(size.width, height))
    ccui.Helper:doLayout(self._root)

    for i,btn in ipairs(self._btns) do
        btn:setVisible(false)
    end

    for i,btnData in ipairs(self._dataList) do
        local btn = self._btns[i]
        if not btn then
            btn = self._btns[1]:clone()
            self._ui.Panel:addChild(btn)
            self._btns[#self._btns + 1] = btn
        end

        local posY = height - (i-1)*self._btnHeight - self._btnHeight * 0.5
        btn:setPositionY(posY)
        btn:setVisible(true)
        local text = btn:getChildByName("Text")
        text:setString(btnData.name)
        btn:addClickEventListener(function()
            executeFunc(self._cb,btnData)
            self:hide()
        end)
    end

    local halfWinHei = display.height/2
    local halfWinWid = display.width/2
    self._ui.Panel:setPosition(pos.x - halfWinWid, pos.y - halfWinHei - height * 0.5)
end


return ClassRef
